package com.ysd.lis.service.lab.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.entity.SysUser;
import com.ysd.lis.entity.lab.LabProcessAttribute;
import com.ysd.lis.entity.lab.LabProcessSet;
import com.ysd.lis.entity.lab.LabUnqualifiedGroup;
import com.ysd.lis.mapper.lab.LabProcessAttributeMapper;
import com.ysd.lis.mapper.lab.LabProcessSetMapper;
import com.ysd.lis.request.DeleteParam;
import com.ysd.lis.service.lab.LabProcessAttributeService;
import com.ysd.util.RedisUserManager;
import com.ysd.util.ToolsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author bai
 * @since 2024-02-20
 */
@Service
public class LabProcessAttributeServiceImpl extends ServiceImpl<LabProcessAttributeMapper, LabProcessAttribute> implements LabProcessAttributeService {

    @Autowired
    LabProcessAttributeMapper processAttributeMapper;
    @Autowired
    LabProcessSetMapper labProcessSetMapper;

    @Override
    public Result getProcessAttributeList(LabProcessAttribute labProcessAttribute) {

        MPJLambdaWrapper<LabProcessAttribute> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.eq(LabUnqualifiedGroup::getDelFlag, 0);
        SysUser sysUser = RedisUserManager.getUser();
        if (ToolsUtils.isNotEmpty(sysUser)) {
            queryWrapper.eq(LabProcessSet::getOrgId, sysUser.getOrgId());
        }
        if (ToolsUtils.isNotEmpty(labProcessAttribute)) {
            if (ToolsUtils.isNotEmpty(labProcessAttribute.getKeywords())) {
                queryWrapper.like(LabProcessAttribute::getProcessAttName, labProcessAttribute.getKeywords());
            }
        }
        queryWrapper.eq(LabProcessAttribute::getProcessSetId, labProcessAttribute.getProcessSetId());
        queryWrapper.orderByAsc(LabProcessAttribute::getSeq);
        List<LabProcessAttribute> list = processAttributeMapper.selectList(queryWrapper);
        return Result.succ(list);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result updateProcessAttribute(LabProcessAttribute labProcessAttribute) {
        if (ToolsUtils.isEmpty(labProcessAttribute)) {
            Result.fail("入参不可为空");
        }
        String processAttValue = labProcessAttribute.getProcessAttValue();
        int delete = 0;
        if (processAttValue.equals("1") && ToolsUtils.isNotEmpty(labProcessAttribute.getId())) { //直接删除
            String id = labProcessAttribute.getId();
            delete = processAttributeMapper.deleteById(id);
            LambdaQueryWrapper<LabProcessAttribute> delePro = new LambdaQueryWrapper<>();
            delePro.eq(LabProcessAttribute::getParentId, labProcessAttribute.getId());
            delete = processAttributeMapper.delete(delePro);
        } else if (processAttValue.equals("0")) {


            return Result.succ("无修改信息");
        }
        if (delete > 0) {
            return Result.fail("修改成功");
        } else {
            return Result.fail("修改失败");
        }
    }

    @Override
    public Result daleteProcessAttribute(DeleteParam deleteParam) {
        if (ToolsUtils.isEmpty(deleteParam)) {
            Result.fail("入参不可为空");
        }
        List<String> ids = Arrays.asList(deleteParam.getIds().split(","));
        //执行删除节点属性信息
        Boolean delete = this.removeByIds(ids);
        if (delete) {
            return Result.fail("删除成功");
        } else {
            return Result.fail("删除失败");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result addProcessAttribute(LabProcessAttribute labProcessAttribute) {
        if (ToolsUtils.isEmpty(labProcessAttribute)) {
            Result.fail("入参不可为空");
        }
        SysUser sysUser = RedisUserManager.getUser();
        //添加时，先删除原有的，让后执行新增
        LambdaQueryWrapper<LabProcessAttribute> delWrapper = new LambdaQueryWrapper<>();
        delWrapper.eq(LabProcessAttribute::getProcessSetId, labProcessAttribute.getProcessSetId());
        int a = processAttributeMapper.delete(delWrapper);
        if (a < 0) {
            return Result.fail("清除原有属性失败");
        }
        boolean save = true;
        if (ToolsUtils.isNotEmpty(labProcessAttribute.getIsCreatReceipt()) && labProcessAttribute.getIsCreatReceipt().equals("1")) {
            LabProcessAttribute labProcessAttributeNew = new LabProcessAttribute();
            labProcessAttributeNew.setOrgId(sysUser.getOrgId());
            labProcessAttributeNew.setProcessSetId(labProcessAttribute.getProcessSetId());
            labProcessAttributeNew.setProcessAttName("是否有生成交接单功能");
            labProcessAttributeNew.setProcessAttValue(labProcessAttribute.getIsCreatReceipt());
            labProcessAttributeNew.setIsAssembly("1");//代表室 是否有生成交接单功能表单
            save = this.save(labProcessAttributeNew);
        }
        if (ToolsUtils.isNotEmpty(labProcessAttribute.getIsPrint()) && labProcessAttribute.getIsPrint().equals("1")) {
            LabProcessAttribute labProcessAttributeNew1 = new LabProcessAttribute();
            labProcessAttributeNew1.setOrgId(sysUser.getOrgId());
            labProcessAttributeNew1.setProcessSetId(labProcessAttribute.getProcessSetId());
            labProcessAttributeNew1.setProcessAttName("是否有打印交接单功能");
            labProcessAttributeNew1.setProcessAttValue(labProcessAttribute.getIsPrint());
            labProcessAttributeNew1.setIsAssembly("2");//代表室 是否有打印交接单功能
            save = this.save(labProcessAttributeNew1);
        }

        if (ToolsUtils.isNotEmpty(labProcessAttribute.getIsInterface()) && labProcessAttribute.getIsInterface().equals("1")) {
            LabProcessAttribute labProcessAttributeNew1 = new LabProcessAttribute();
            labProcessAttributeNew1.setOrgId(sysUser.getOrgId());
            labProcessAttributeNew1.setProcessSetId(labProcessAttribute.getProcessSetId());
            labProcessAttributeNew1.setProcessAttName("是否有界面");
            labProcessAttributeNew1.setProcessAttValue(labProcessAttribute.getIsInterface());
            labProcessAttributeNew1.setIsAssembly("3");//代表室 是否有打印交接单功能
            save = this.save(labProcessAttributeNew1);
        }

        if (ToolsUtils.isNotEmpty(labProcessAttribute.getIsBilling()) && labProcessAttribute.getIsBilling().equals("1")) {
            LabProcessAttribute labProcessAttributeNew1 = new LabProcessAttribute();
            labProcessAttributeNew1.setOrgId(sysUser.getOrgId());
            labProcessAttributeNew1.setProcessSetId(labProcessAttribute.getProcessSetId());
            labProcessAttributeNew1.setProcessAttName("是否计费");
            labProcessAttributeNew1.setProcessAttValue(labProcessAttribute.getIsBilling());
            labProcessAttributeNew1.setIsAssembly("4");//代表室 是否有打印交接单功能
            save = this.save(labProcessAttributeNew1);
        }

        if (ToolsUtils.isNotEmpty(labProcessAttribute.getIsSysAtt()) && labProcessAttribute.getIsSysAtt().equals("1")) {
            LabProcessAttribute labProcessAttributeNew1 = new LabProcessAttribute();
            labProcessAttributeNew1.setOrgId(sysUser.getOrgId());
            labProcessAttributeNew1.setProcessSetId(labProcessAttribute.getProcessSetId());
            labProcessAttributeNew1.setProcessAttName("是否系统节点");
            labProcessAttributeNew1.setProcessAttValue(labProcessAttribute.getIsSysAtt());
            labProcessAttributeNew1.setIsAssembly("5");//代表室 是否有打印交接单功能
            save = this.save(labProcessAttributeNew1);
        }

        if (ToolsUtils.isNotEmpty(labProcessAttribute.getProcessAttValue()) && labProcessAttribute.getProcessAttValue().equals("1")) {
            LabProcessAttribute labProcessAttributeNew0 = new LabProcessAttribute();
            labProcessAttributeNew0.setOrgId(sysUser.getOrgId());
            labProcessAttributeNew0.setProcessSetId(labProcessAttribute.getProcessSetId());
            labProcessAttributeNew0.setIsAssembly("0");
            labProcessAttributeNew0.setProcessAttName("是否有标本退回功能");
            labProcessAttributeNew0.setProcessAttValue(labProcessAttribute.getProcessAttValue());
            save = this.save(labProcessAttributeNew0);
            if (ToolsUtils.isNotEmpty(labProcessAttribute.getAssemblyValue())) {
                labProcessAttribute.setOrgId(sysUser.getOrgId());
                labProcessAttribute.setParentId(labProcessAttributeNew0.getId());
                labProcessAttribute.setProcessAttValue(labProcessAttribute.getAssemblyValue());
                labProcessAttribute.setProcessAttName(labProcessAttribute.getAssemblyName());
                this.save(labProcessAttribute);
            }
        }
        if (save) {
            return Result.succ("保存节点属性成功");
        } else {
            return Result.fail("保存节点属性失败");
        }
    }

    @Override
    public Result getProcessAttributeListByCode(String code) {
        MPJLambdaWrapper<LabProcessSet> queryWrapper = new MPJLambdaWrapper<>();
        SysUser sysUser = RedisUserManager.getUser();
        if (ToolsUtils.isNotEmpty(sysUser)) {
            queryWrapper.eq(LabProcessSet::getOrgId, sysUser.getOrgId());
        }
        queryWrapper.eq(LabProcessSet::getDelFlag, 0);
        queryWrapper.eq(LabProcessSet::getCode, code);
        LabProcessSet labProcessSet = labProcessSetMapper.selectOne(queryWrapper);
        List<LabProcessAttribute> list = new ArrayList<>();
        if (ToolsUtils.isNotEmpty(labProcessSet)) {
            String id = labProcessSet.getId();
            MPJLambdaWrapper<LabProcessAttribute> queryAttributeWrapper = new MPJLambdaWrapper<>();
            queryAttributeWrapper.eq(LabProcessAttribute::getProcessSetId, id);
            queryAttributeWrapper.eq(LabProcessAttribute::getDelFlag, 0);
            if (ToolsUtils.isNotEmpty(sysUser)) {
                queryAttributeWrapper.eq(LabProcessAttribute::getOrgId, sysUser.getOrgId());
            }
            list = processAttributeMapper.selectList(queryAttributeWrapper);
        }
        return Result.succ(list);
    }
}
